package com.net.xpay.core.manager.impl.safePay;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.core.dto.AccountAmountCountDto;
import com.net.xpay.core.model.*;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.safePay.SafePayBatchMapper;
import com.net.xpay.core.domain.safePay.SafePayBatch;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class SafePayBatchManagerImpl extends AbstractVersionedManager<SafePayBatch> implements SafePayBatchManager{

    @Autowired
    private SafePayBatchMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(SafePayBatch obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }


    @Override
    protected BasicMapper<Long, SafePayBatch> getMapper() {
        return mapper;
    }

    @Override
    public SafePayBatch findByImportBatchNo(Long userId, String importBatchNo) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(importBatchNo));
        SafePayBatch condition = new SafePayBatch();
        condition.setUserId(userId);
        condition.setImportBatchNo(importBatchNo);

        return getByCondition(condition);
    }

    @Override
    public List<Long> listNeedTransBatch() {
        List<Long> result = mapper.listNeedTransBatch();
        if (CollectionUtils.isEmpty(result)) {
            return new ArrayList<>();
        }
        return result;
    }

    @Override
    public SafePayBatch findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        SafePayBatch condition = new SafePayBatch();
        condition.setUserId(userId);
        condition.setId(id);

        return getByCondition(condition);
    }

    @Override
    public List<Long> listIds(SafePayBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        List<Long> ids = mapper.listIds(searchModel);
        return ids == null ? new ArrayList<>() : ids;
    }

    @Override
    public List<SafePayBatch> search(SafePayBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("create_time", "desc");
        page.addOrder("id", "desc");

        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public List<SafePayBatch> list(SafePayBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public long searchCount(SafePayBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public UserSafePayTransStatModel countAmountAndTimesOfSuccess(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countAmountAndTimesOfSuccess(userId, minEndTime, maxEndTime);
    }

    @Override
    public CompanyBillDailyStatModel statDailyCompanyBill(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(minEndTime != null && maxEndTime != null);
        return mapper.statDailyCompanyBill(minEndTime, maxEndTime);
    }

    @Override
    public SafePayBatchStatResultModel statLevelOneSuccessSafePayBatch(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.statLevelOneSuccessSafePayBatch(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public SafePayBatchStatResultModel statLevelTwoSuccessSafePayBatch(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.statLevelTwoSuccessSafePayBatch(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public BigDecimal getTransferLimit(LocalDateTime minEndTime, LocalDateTime maxEndTime, Long userId, Long safePayBookAccountId) {
        Preconditions.checkArgument(minEndTime != null && maxEndTime != null && userId != null && safePayBookAccountId != null);
        return mapper.getTransferLimit(minEndTime, maxEndTime, userId, safePayBookAccountId);
    }

    @Override
    public UserSafePayTransStatCommonModel countAmountAndTimesOfSuccessBank(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countAmountAndTimesOfSuccessBank(userId, minEndTime, maxEndTime);
    }

    @Override
    public UserSafePayTransStatCommonModel countAmountAndTimesOfSuccessAlipay(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countAmountAndTimesOfSuccessAlipay(userId, minEndTime, maxEndTime);
    }

    @Override
    public UserSafePayTransStatCommonModel countAmountAndTimesOfSuccessNotFreeBank(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countAmountAndTimesOfSuccessNotFreeBank(userId, minEndTime, maxEndTime);
    }

    @Override
    public UserSafePayTransStatCommonModel countAmountAndTimesOfSuccessNotFreeAlipay(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countAmountAndTimesOfSuccessNotFreeAlipay(userId, minEndTime, maxEndTime);
    }

    @Override
    public PartnerUserSafePayTransDayStatModel sumPartnerUserSafePayTransDay(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.sumPartnerUserSafePayTransDay(userId, minEndTime, maxEndTime);
    }

    @Override
    public PartnerUserSafePayTransDayStatModel sumPartnerSafePayTransDay(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.sumPartnerSafePayTransDay(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public SafePayTransDailyStatModel statActivePartnerCount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {

        return mapper.statActivePartnerCount(minEndTime, maxEndTime);
    }

    @Override
    public CompanySafePayTransDayStatModel sumCompanySafePayTransDay(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        return mapper.sumCompanySafePayTransDay(minEndTime, maxEndTime);
    }

    @Override
    public long sumTotalVeryAlipayUserCount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {

        return mapper.sumTotalVeryAlipayUserCount(minEndTime, maxEndTime);
    }

    @Override
    public long sumUserTotalVeryAlipayUserCount(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument( userId != null && minEndTime != null && maxEndTime != null);
        return mapper.sumUserTotalVeryAlipayUserCount(userId,minEndTime, maxEndTime);
    }

    @Override
    public SafePayTransBankDailyStatModel statSafePayTransBankAmount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        return mapper.statSafePayTransBankAmount(minEndTime, maxEndTime);
    }

    /**
     *统计 转账金额
     *  @param startTime
     * @param endTime
     * @param id
     * @param dayTime
     */
    @Override
    public AccountAmountCountDto sumSafeSuccessAmount(LocalDateTime startTime, LocalDateTime endTime, Long id, LocalDateTime dayTime) {
        return mapper.sumSafeSuccessAmount(startTime, endTime, id,dayTime);
    }
}
